﻿using Neoit.Utils.Extensions;

namespace Neoit.Utils.TemplateParser
{
    /// <summary>
    /// ParseManager
    /// </summary>
    public class ParseManager
    {

        /* 1.变量: ${name}
         * 2.if: <if $='age==18'></if>
         * 3.list: <list $='item in list.xx'> </list>
         */

        /// <summary>
        /// 解析带变量的字符串，如: hello ${name}!
        /// </summary>
        /// <typeparam name="T">对象值：dynamic、Class</typeparam>
        /// <param name="template">带变量的字符串</param>
        /// <param name="t">对象值：dynamic、Class</param>
        /// <returns></returns>
        public static string Parse<T>(string template, T t)
        {
            if (!template.Has()|| t==null) return template;
            template = ParseList.Parse(template, t);
            template = ParseIf.Parse(template, t);
            template = ParseVar.Parse(template, t);
            return template;
        }
        /// <summary>
        /// 解析带变量的字符串，如: hello ${name}!
        /// </summary>
        /// <typeparam name="T">对象值：dynamic、Class</typeparam>
        /// <param name="template">带变量的字符串</param>
        /// <param name="t">对象值：dynamic、Class</param>
        /// <param name="fullPath">Layout</param>
        /// <returns></returns>
        public static string ParseWithLayout<T>(string template, T t, string fullPath = null)
        {
            if (!template.Has() || t == null) return template;
            template = ParseList.Parse(template, t);
            template = ParseIf.Parse(template, t);
            template = ParseVar.Parse(template, t);
            template = ParsePart.Parse(template, t, fullPath);
            return template;
        }
    }
}
